package com.lxy.cxcyxmmsbackend.sys.service.impl;

import com.lxy.cxcyxmmsbackend.sys.entity.QxglRole;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.NormalException;
import com.lxy.cxcyxmmsbackend.sys.mapper.CommonMapper;
import com.lxy.cxcyxmmsbackend.sys.mapper.PermissionMapper;
import com.lxy.cxcyxmmsbackend.sys.mapper.QxglRoleMapper;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.QxglRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘新颖
 * @since 2024-01-07
 */
@Service
public class QxglRoleServiceImpl extends ServiceImpl<QxglRoleMapper, QxglRole> implements QxglRoleService {
    @Resource
    QxglRoleMapper qxglRoleMapper;
    @Resource
    PermissionMapper permissionMapper;
    @Resource
    CommonMapper commonMapper;
    @Override
    @Transactional
    public Result selectRole(String roleMc) {
        List<QxglRole> roles = qxglRoleMapper.selectRole(roleMc);
        if (roles == null || roles.size() == 0) {
            throw new NormalException("未查询到该角色");
        }
        return Result.success("查询角色成功", roles);
    }

    @Override
    @Transactional
    public Result selectRoleNoByUid(String userBh) {
        String roleBh = qxglRoleMapper.selectRoleByUser_id(userBh).getRoleBh();
        List<QxglRole> roles = qxglRoleMapper.selectRoleNoByRId(roleBh);
        if (roles == null || roles.size() == 0) {
            throw new NormalException("查询其他角色失败");
        }
        return Result.success("查询其他角色成功", roles);
    }

    @Override
    @Transactional
    public Result insertRole(QxglRole qxglRole) {
        qxglRole.setRoleCjsj(new Date(System.currentTimeMillis()));
        String roleBh = commonMapper.getNewBhRole();
        qxglRole.setRoleBh(roleBh);
        int i = qxglRoleMapper.insertRole(qxglRole);
        if (i != 1) {
            throw new NormalException("新增角色失败");
        }
        return Result.success("新增角色成功", qxglRole);
    }

    @Override
    @Transactional
    public Result updateRole(QxglRole qxglRole) {

        qxglRole.setRoleGxsj(new Date(System.currentTimeMillis()));
        int i = qxglRoleMapper.updateRole(qxglRole);
        if (i != 1) {
            throw new NormalException("修改角色失败");
        }
        return Result.success("修改角色成功", qxglRole);
    }

    @Override
    @Transactional
    public Result deleteRole(String roleBh) {
        if ("R2001".equals(roleBh)) {
            throw new NormalException("请勿删除系统管理员！！");
        }
        int i = qxglRoleMapper.deleteRole(roleBh);
        if (i != 1) {
            throw new NormalException("删除角色失败");
        }
        int i1 = permissionMapper.deleteRARelByroleId(roleBh);
        int i2 = permissionMapper.deleteRmRelByroleId(roleBh);
        return Result.success("删除角色成功", roleBh);
    }
}
